<template>
	<view class="container">
		<view class="uni-list">
			<view class="uni-list-cell uni-list-cell-pd">
				<view>姓名</view>
				<view class="uni-list-cell-db"><input type="text" v-model="receiveName" class="uni-input"
						placeholder="请输入" /></view>
			</view>
			<view class="uni-list-cell uni-list-cell-pd">
				<view>电话</view>
				<view class="uni-list-cell-db"><input type="number" v-model="receivePhone" class="uni-input"
						placeholder="请输入" /></view>
			</view>
			<view class="uni-list-cell uni-list-cell-pd">
				<view>地址</view>
				<view class="uni-list-cell-db" @click="show = !show">
					<u-picker :show="show" ref="uPicker" :loading="loading" :columns="columns" keyName="areaName"
						@change="changeHandler" itemHeight="88" @confirm="confirm"></u-picker>
					<view class="uni-input" type="text" style="padding: 15rpx 0;">{{citystr}}</view>
				</view>
				<view class="uni-navigate-right"></view>
			</view>
			<view class="uni-list-cell uni-list-cell-pd">
				<view>公司详细地址</view>
				<view class="uni-list-cell-db"><input type="text" class="uni-input" v-model="address"
						placeholder="请输入详细地址" /></view>
			</view>
			<view class="uni-list-cell uni-list-cell-pd">
				<view>设为默认</view>
				<view>
					<switch color="#06A972" :checked="isdefault == 'Y' ? true : false" @change="switchChange">
					</switch>
				</view>
			</view>
		</view>

		<view class="footer">
			<u-button type="primary" color="#06A972" text="确定" @click="saveAddress"></u-button>
		</view>

	</view>
</template>


<script>
	export default {
		data() {
			return {
				show: false,
				loading: false,
				columns: [
					[],
					[],
					[]
				],
				provinceList: [],
				cityList: [],
				areaList: [],
				defaultProvince: 110000, //北京市
				defaultCity: 110100, //北京市
				isdefault: 'N',
				citystr: '请选择所在地区',
				provinceCode: 0,
				cityCode: 0,
				districtCode: 0,
				receiveName: '',
				receivePhone: '',
				addressId: 0,
				address: '',
			}

		},
		onLoad(option) {
			let that = this;
			if (option.addressId > 0) {
				uni.setNavigationBarTitle({
					title: '编辑地址'
				})
				that.getAddressDetail(option.addressId);
				that.addressId = option.addressId;
			} else {
				that.defaultAddress();
			}
		},
		methods: {
			changeHandler(e) {
				let that = this;
				const {
					columnIndex,
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = that.$refs.uPicker,
					value
				} = e
				if (columnIndex === 0) {
					that.loading = true
					// 获取市
					that.getAddress(value[columnIndex].areaId).then(res => {
						picker.setColumnValues(1, res)

						// 获取区
						that.getAddress(res[columnIndex].areaId).then(res => {
							picker.setColumnValues(2, res)
							that.loading = false
						})
					})
				}
				if (columnIndex === 1) {
					// 获取区
					that.getAddress(value[columnIndex].areaId).then(res => {
						picker.setColumnValues(2, res)
						that.loading = false
					})
				}
			},

			defaultAddress() {
				let that = this;
				// 获取所有省
				that.getAddress(1).then(res => {
					that.provinceList = res

					// 获取默认市
					that.getAddress(that.defaultProvince).then(res => {
						that.cityList = res

						// 获取默认区
						that.getAddress(that.defaultCity).then(res => {
							that.areaList = res
							that.columns = [
								[...that.provinceList],
								[...that.cityList],
								[...that.areaList]
							];
						});
					});
				});
			},
			
			getAddressDetail(id) {
				let that = this;
				that.$request.get('userAddr/getAddress', {
					addressId: id,
					userInfoId: that.$store.state.app.uid
				}).then(res => {
					that.receiveName = res.value.receiveName;
					that.receivePhone = res.value.receivePhone;
					that.citystr = res.value.provinceName + '/' + res.value.cityName + '/' + res.value.districtName;
					that.address = res.value.address;
					that.isdefault = res.value.isDefault;
					that.provinceCode = res.value.provinceCode;
					that.cityCode = res.value.cityCode;
					that.districtCode = res.value.districtCode;
				}).catch(err => {
					that.$util.Tips({
						title: err.msg
					});
				})
			},

			getAddress(id) {
				return new Promise((resolve, reject) => {
					let that = this;
					that.$request.get('area/getAreaList', {
						parentId: id
					}).then(res => {
						resolve(res.value);
					}).catch(err => {
						that.$util.Tips({
							title: err.msg
						});
					})
				});
			},

			confirm(e) {
				console.log('confirm', e)
				let that = this;
				that.provinceCode = e.value[0].areaId;
				that.cityCode = e.value[1].areaId;
				that.districtCode = e.value[2].areaId;
				that.citystr = e.value[0].areaName + '/' + e.value[1].areaName + '/' + e.value[2].areaName;
				this.show = false;
			},

			switchChange(e) {
				let value = e.detail.value;
				this.isdefault = value ? 'Y' : 'N';
			},

			saveAddress() {
				let that = this,
					data = {};
				if (that.receiveName == '') {
					return that.$util.Tips({
						title: '请输入姓名'
					});
				}
				if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.receivePhone)) {
					return that.$util.Tips({
						title: '请输入正确的手机号码'
					});
				}
				if (that.provinceCode == 0) {
					return that.$util.Tips({
						title: '请选择地区'
					});
				}
				if (that.address == '') {
					return that.$util.Tips({
						title: '请输入详细地址'
					});
				}

				data = {
					provinceCode: that.provinceCode,
					cityCode: that.cityCode,
					districtCode: that.districtCode,
					isDefault: that.isdefault,
					userInfoId: that.$store.state.app.uid,
					receiveName: that.receiveName,
					receivePhone: that.receivePhone,
					address: that.address
				};

				if (that.addressId) {
					data.addressId = that.addressId;
				}
				
				that.$request.get('userAddr/save', data).then(res => {
					that.$util.Tips({
						title: '保存成功'
					}, {
						tab: 3
					})
				}).catch(err => {
					that.$util.Tips({
						title: err.msg
					})
				})
			}
		}
	}
</script>

<style>
	.uni-list::before,
	.uni-list::after {
		display: none;
	}

	.uni-list .uni-input {
		text-align: right;
	}

	.container .bottom {
		position: fixed;
		z-index: 99;
		left: 0;
		bottom: 0;
		width: 100%;
		box-sizing: border-box;
		padding: 70rpx;
	}

	.showimg_box {
		display: flex;
		align-items: center;
		flex-wrap: wrap;

	}

	.images {
		width: 208rpx;
		height: 208rpx;
	}

	.imgarr {
		display: flex;
		align-items: center;
		position: relative;
		margin: 20rpx 20rpx 0 0;
	}

	.icon-cuo {
		position: absolute;
		right: 0;
		top: 0;
		z-index: 998;
	}
</style>